package com.cracker.sso.service;

import com.cracker.sso.dao.TokenDao;
import com.cracker.sso.dto.response.UserDto;
import com.cracker.sso.mapper.UserMapper;
import com.cracker.sso.model.TokenModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class TokenService {
    @Autowired
    private TokenDao tokenDao;
    @Autowired
    private UserMapper userMapper;

    public TokenModel get(String token) {
        return tokenDao.get(token);
    }

    public UserDto query(String token) {
        TokenModel tokenModel = tokenDao.get(token);
        if (null == tokenModel) {
            return null;
        }
        return userMapper.modelToDto(tokenModel.getUser());
    }

    public void delete(String token) {
        tokenDao.delete(token);
    }
}
